/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.media.MediaException;

/**
 * @author Abahe
 */
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

public class TestMIDlet extends MIDlet {

    static TestMIDlet instance;
    Player sound01;
    BackgroundMusic bgMusic;
    public static boolean BGSOUND = false;
    //Player sound02;

    public TestMIDlet() {
        TestMIDlet.instance = this;
        bgMusic = new BackgroundMusic(this);
    }

    public void startApp() {
        Display.getDisplay(this).setCurrent(new testSound());
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void startSound() {
        try {
            sound01.start();
            BGSOUND = false;
        } catch (MediaException ex) {
        }
    }

    public class testSound extends Canvas implements CommandListener, PlayerListener {

        private Command exit;
        private Command shout;
        long delay;

        public testSound() {
            exit = new Command("Exit", Command.EXIT, 1);
            addCommand(exit);
            shout = new Command("Shout!!!", Command.OK, 1);
            addCommand(shout);

            setCommandListener(this);

            try {
                sound01 = Manager.createPlayer(getClass().getResourceAsStream("/ranch.mid"), "audio/midi");
                sound01.realize();
                sound01.prefetch();
                sound01.addPlayerListener(this);
                sound01.setLoopCount(-1);
                sound01.start();

                /*
                sound02 = Manager.createPlayer(getClass().getResourceAsStream("/battle.mid"), "audio/midi");
                sound02.realize();
                sound02.prefetch();
                sound02.addPlayerListener(this);
                sound02.setLoopCount(-1);
                 * 
                 */
                //sound02.start();
            } catch (Exception e) {
            }
        }

        public void playerUpdate(Player arg0, String eventType, Object arg2) {
            if (eventType.equals(STARTED)) {
                delay = System.currentTimeMillis() - delay;
                repaint();
            } else if (eventType.equals(END_OF_MEDIA)) {
                delay = System.currentTimeMillis();
            }
        }

        public void commandAction(Command command, Displayable displayable) {
            if (command == exit) {
                TestMIDlet.instance.notifyDestroyed();
            } else if (command == shout) {
                /*
                try {

                    
                    try {
                    sound02.start();
                    } catch (MediaException ex) {
                    }
                    
                    sound01.stop();
                    if (BGSOUND == false) {
                        BGSOUND = true;
                        bgMusic.start();
                    }
                    //bgMusic.playSound();
                } catch (MediaException ex) {
                }
                 * 
                 */
                bgMusic.playSound();
            }
        }

        protected void paint(Graphics g) {
            g.setColor(0xffffff);
            g.fillRect(0, 0, getWidth(), getHeight());

            g.setColor(0);
            g.drawString("delay: " + delay, 0, 0, 0);
        }
    }
}
